package cheqex.update;

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

import cheqex.update.controllers.*;

public class LogInView extends JFrame{

	private JTextField userName;
	private JPasswordField password;
	private JButton nextButton;
	private JLabel username;
	private JLabel pass;
	private boolean clicked = false;
	
	public LogInView(){
		this.initializeComponents();
	}
	
	//closes login frame
	private void closeframe(){
		this.dispose();
	}
	
	private void requestLogIn(){
		String user = userName.getText();
		char pw[] = password.getPassword();
         if(AccountHandler.AccountCheck(user, pw)){
	        	closeframe();
	    		CheqexFrame cf = new CheqexFrame();
	    		cf.setVisible(true);
	    		cf.pack();
         }else{ 
	       	password.setText(null);
			userName.setText(null);
			if(AccountHandler.UserNameExists(user)) //wrong password
					JOptionPane.showMessageDialog(null, "Invalid Password.", "Error", JOptionPane.WARNING_MESSAGE);
			else //wrong user name
					JOptionPane.showMessageDialog(null, "Invalid Username.", "Error", JOptionPane.WARNING_MESSAGE);
	        }
	}
	
	//initializes components
	private void initializeComponents(){
		userName = new JTextField(20);
		userName.addKeyListener(new KeyListener(){

			@Override
			public void keyPressed(KeyEvent e) {
				if(e.getKeyCode()==KeyEvent.VK_ENTER){
					requestLogIn();
				}
				
			}

			@Override
			public void keyReleased(KeyEvent e) {
				
			}

			@Override
			public void keyTyped(KeyEvent e) {
				// TODO Auto-generated method stub
				
			}
			
		});

		
		password = new JPasswordField(20);
		password.addKeyListener(new KeyListener(){

			@Override
			public void keyPressed(KeyEvent e) {
				if(e.getKeyCode()==KeyEvent.VK_ENTER){
					requestLogIn();
				}
				
			}

			@Override
			public void keyReleased(KeyEvent e) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void keyTyped(KeyEvent e) {
				// TODO Auto-generated method stub
				
			}
			
		});

		nextButton = new JButton("Log In");
		nextButton.setActionCommand("loggingIn");
				nextButton.addActionListener(new ActionListener(){
		@Override
		public void actionPerformed(ActionEvent e) {
			if(e.getActionCommand().equals("loggingIn")){
				String user = userName.getText();
				char pw[] = password.getPassword();
		         if(AccountHandler.AccountCheck(user, pw)){
			        	closeframe();
			    		CheqexFrame cf = new CheqexFrame();
			    		cf.setVisible(true);
			    		cf.pack();
		         }else{ 
			       	password.setText(null);
					userName.setText(null);
					if(AccountHandler.UserNameExists(user)) //wrong password
							JOptionPane.showMessageDialog(null, "Invalid Password.", "Error", JOptionPane.WARNING_MESSAGE);
					else //wrong user name
							JOptionPane.showMessageDialog(null, "Invalid Username.", "Error", JOptionPane.WARNING_MESSAGE);
			        }
				}
			}//end of action performed
		}); //end of addActionListener
		
		username = new JLabel("username: ");
		username.setLabelFor(userName);
		
		pass = new JLabel("password: ");
		pass.setLabelFor(password);
		
		JPanel panel = new JPanel();
		panel.add(this.username);
		panel.add(this.userName);
		panel.add(this.pass);
		panel.add(this.password);
		panel.add(this.nextButton);
		this.add(panel);
		this.setVisible(true);
		this.pack();
		this.setResizable(false);
		this.setTitle("CheQEx");
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}
}
